package cn.texous.util.commons.util;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * insert description here
 *
 * @author Showa.L
 * @since 2019/7/5 10:52
 */
public class ReplaceUtils {

    /***/
    public static final String DOLLAR_PARENTHESIS_START = "${";
    /***/
    public static final String HASHTAG_PARENTHESIS_START = "#{";
    /***/
    public static final String PARENTHESIS_START = "{";
    /***/
    public static final String PARENTHESIS_END = "}";
    /***/
    public static final String DOUBLE_COLON = "::";


    /**
     * 符号替换
     *
     * @param replaceParam 参数
     * @return
     */
    public static String replace(ReplaceParam replaceParam) {
        String resource = replaceParam.getResource();
        Set<Map.Entry<String, String>> entrySet =
                Optional.ofNullable(replaceParam.getParams())
                        .map(Map::entrySet)
                        .orElse(new HashSet<>());
        String start = replaceParam.getIdentifierStart() == null
                ? DOLLAR_PARENTHESIS_START
                : replaceParam.getIdentifierStart();
        String end = replaceParam.getIdentifierEnd() == null
                ? PARENTHESIS_END
                : replaceParam.getIdentifierEnd();
        for (Map.Entry<String, String> item : entrySet) {
            String target = new StringBuilder()
                    .append(start)
                    .append(item.getKey())
                    .append(end).toString();
            resource = resource.replace(target, item.getValue());
        }

        return resource;
    }

    /**
     * 将 map 的 key 拼接上 start 和 end
     * @param map
     * @param start
     * @param end
     * @return
     */
    public static Map<String, String> toRegexKeyMap(Map<String, String> map, String start, String end) {
        Map<String, String> result = new HashMap<>();
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String target = new StringBuilder()
                    .append(start)
                    .append(entry.getKey())
                    .append(end).toString();
            result.put(target, entry.getValue());
        }
        return result;
    }

    /***/
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ReplaceParam {
        // 需要替换字符的内容
        private String resource;
        // 参数列表
        private Map<String, String> params;
        // 标识符开头
        private String identifierStart;
        // 标识符结尾
        private String identifierEnd;
    }

    public static void main(String[] args) {
        Map<String, String> params = new HashMap<>();
        params.put("test", "hehehe");
        params.put("name", "小明");
        params.put("talk", "今天很开心");
        ReplaceParam param = ReplaceParam.builder()
                .resource("测试 ${name} 说：${talk}, ${name}有一些小小的${mohion}")
                .params(params)
                .identifierStart("${")
                .identifierEnd("}")
                .build();
        System.out.println(System.currentTimeMillis());
        String target = replace(param);
        System.out.println(System.currentTimeMillis());
        System.out.println(target);
    }

}
